<?php

	class user {
		
		private $pdo;
	
		function __construct($conn)
		{
			// Create a connection to the database.
			$this->pdo = $conn;
		}
		
		function setRole($currentUser, $admin){
			$success = true;
			$msg = "";
			
			try {
				$this->pdo->beginTransaction();
				$stmt = $this->pdo->prepare("UPDATE UTENTE SET admin=".$admin." WHERE id_str=".$currentUser);
				$stmt->execute();
				$this->pdo->commit();
				
			}catch (Exception $e){
				$success = false;
				$msg = $e->getMessage();
			}
			
			$o = array("success"=>$success, "msg"=>$msg);
			
			return $o;
		}
		
		function checkUser($username, $pwd){
			
			$this->pdo->beginTransaction();
			
			$stmt = $this->pdo->prepare("SELECT * FROM UTENTE 
			 WHERE  
				username = '".$username."' AND 
				password = '".$pwd."' ");
			$stmt->execute();
			
			$this->pdo->commit();
			
			return $stmt->fetchAll();
		}
		
		function listUser(){
			
			$this->pdo->beginTransaction();
			
			$stmt = $this->pdo->prepare("SELECT * FROM UTENTE ");
			$stmt->execute();
			
			$this->pdo->commit();
			
			return $stmt->fetchAll();
		}
		
		function registra($data) {
			$success = true;
			$msg = "";
			
			try {
				$username = $_POST["username"];
				$password = $_POST["password"];
				$struttura = $_POST["struttura"];
				$citta = $_POST["citta"];
				$addr = $_POST["addr"];
				$cap = $_POST["cap"];
				$email = $_POST["email"];
				$nome = $_POST["nome"];
				$cognome = $_POST["cognome"];
				
				$this->pdo->beginTransaction();
			
				$stmt = $this->pdo->prepare("INSERT INTO UTENTE 
				(username, password, nome, cognome, struttura, citta, addr, cap, email) 
				VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ");
				
				$stmt->execute( array($username, $password, 
				strtoupper($nome), strtoupper($cognome), strtoupper($struttura), 
					strtoupper($citta), strtoupper($addr), $cap, $email));
				
				$this->pdo->commit();
				
			}catch (Exception $e) {
				$success = false;
				$msg = $e->getMessage();
			}
			
			$o = array("success"=>$success, "msg"=>$msg);
			
			return $o;
		}
		
		
	}
?>